import { legacy_createStore as createStore, applyMiddleware, combineReducers } from 'redux'
import cartReducer from './modules/cart'
import logger from 'redux-logger'

// 合并reducer
const rootReducer = combineReducers({
  cart: cartReducer
})

// 中间件
const rootMiddlewares = applyMiddleware(logger)
// 创建仓库
const store = createStore(rootReducer, rootMiddlewares)

// 订阅仓库 dispatch action
store.subscribe(() => {
  // 获取仓库数据
  const state = store.getState();
  // 当 dispatch action 吧购物车数据存储到本地存储
  localStorage.setItem('cartList', JSON.stringify(state.cart.cartList))
})

export default store
